<?php

class Model_City extends Model_Pandrade {

    const TABLE = "loc_city";

    /**
     * @var protected $id id value
     * @method int get_id() get_id(void)
     * @method void set_id() set_id(int $id)
     */
    protected $id;

    /**
     * @var protected $name name value
     * @method string get_name() get_name(void)
     * @method void set_name() set_name(string $name)
     */
    protected $name;

    /**
     * @var protected $state state value
     * @method int get_state() get_state(void)
     * @method void set_state() set_state(int $state)
     */
    protected $state;

    /**
     * @return Model_Loc_city
     */
    public function __construct($id = 0) {
        if (0 < intval($id)) {
            $db = DB::select("*")->from(self::TABLE)->where("id", "=", $id)->limit(1);
            $db = $db->as_assoc()->execute();
            if ($db->count() > 0) {
                $this->load($db->current());
            }
        }
    }

    protected static function get_database_fields() {
        return array(
            'name' => __("Loc_city_Name"),
            'state' => __("Loc_city_State"),
        );
    }

    public function validate() {
        $validateArray = array();
        foreach (self::get_database_fields() as $key => $value) {
            $validateArray[$key] = $this->get($key);
        }
        $validation = Validation::factory($validateArray)
        ;
        if ($validation->check()) {
            return $validateArray;
        } else {
            throw new Validation_Exception('validation', $validation);
        }
    }

    public static function get_report() {
        return DB::select(
                                "id"
                                , "name"
                                , "state"
                                , array("id", "actions"))
                        ->from(self::TABLE)
                        ->order_by("id", "ASC");
    }

    public static function search_city($city = NULL, $state_id = NULL) {
        if ($city === NULL || $state_id===NULL) {
            return array();
        } else if ($city == "") {
            $cities = DB::select(self::TABLE . ".id", self::TABLE . ".name", Model_State::TABLE . ".acronym")->from(self::TABLE)
                    ->join(Model_State::TABLE)
                    ->on(self::TABLE . ".state", "=", Model_State::TABLE . ".id");
            if ($state_id != "") {
                $cities->where(self::TABLE . ".state", "=", $state_id);
            }
            $cities->limit(15)->order_by(self::TABLE . ".name", "ASC");
            return $cities->execute()->as_array();
        } else {
            //expressions with more than word be like: "%WORD1%WORD2%" (for mysql)
            $words = explode(" ", $city);
            $size = sizeof($words);
            $i = $size;
            $mysql_pattern = "";
            do {
                $mysql_pattern .= $words[$size - $i];
                $mysql_pattern .= "%";
                $i--;
            } while ($i > 0);
            $cities = DB::select(self::TABLE . ".id", self::TABLE . ".name", Model_State::TABLE . ".acronym")->from(self::TABLE)
                    ->join(Model_State::TABLE)
                    ->on(self::TABLE . ".state", "=", Model_State::TABLE . ".id")
                    ->where(self::TABLE . ".name", "LIKE", $mysql_pattern);
            if ($state_id != "") {
                $cities->and_where(self::TABLE . ".state", "=", $state_id);
            }
            $cities->limit(15)->order_by(self::TABLE . ".name", "ASC");//->execute()->as_array();
            return $cities->execute()->as_array();
        }
    }

}